package com.cheney.mapboxexample.mapbox;

import com.google.gson.Gson;

import java.util.List;

public class MapGeoJson {
    /**
     * GeoJSON是一种对各种地理数据结构进行编码的格式。GeoJSON对象可以表示几何、特征或者特征集合。
     * 样例：https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson
     * type : FeatureCollection
     * crs : {"type":"name","properties":{"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}}
     * features : [{"type":"Feature","properties":{"id":"ak16994521","mag":2.3,"time":1507425650893,"felt":null,"tsunami":0},"geometry":{"type":"Point","coordinates":[-151.5129,63.1016,0]}},{"type":"Feature","properties":{"id":"ak16994519","mag":1.7,"time":1507425289659,"felt":null,"tsunami":0},"geometry":{"type":"Point","coordinates":[-150.4048,63.1224,105.5]}},{"type":"Feature","properties":{"id":"ak16994517","mag":1.6,"time":1507424832518,"felt":null,"tsunami":0},"geometry":{"type":"Point","coordinates":[-151.3597,63.0781,0]}}]
     */

    private final String type = "FeatureCollection";    // MapGeoJson类型
    private final CrsBean crs = new CrsBean();  // [可选]坐标参考系
    private List<FeaturesBean> features;        // 特征对象的集合

    public List<FeaturesBean> getFeatures() {
        return features;
    }

    public void setFeatures(List<FeaturesBean> features) {
        this.features = features;
    }

    public static class CrsBean {
        /**
         * 非空的CRS对象有两个强制拥有的对象:“type"和"properties”。
         * type表明Crs对象类型（名字Crs 或者 链接Crs）
         * type : name
         * properties : {"name":"urn:ogc:def:crs:OGC:1.3:CRS84"}
         */
        private final String type = "name";
        private final CrsProperties properties = new CrsProperties();

        public static class CrsProperties {
            /**
             * 名字Crs的Properties必须为 name
             * name : urn:ogc:def:crs:OGC:1.3:CRS84
             */
            private final String name = "urn:ogc:def:crs:OGC:1.3:CRS84";  // 标识坐标参考系统的字符串
        }
    }

    // 特征对象
    public static class FeaturesBean {
        /**
         * 类型为"Feature"的GeoJSON对象是特征对象
         * type : Feature
         * properties : {"type":0,"id":"ak16994521","mag":2.3,"time":1507425650893,"felt":null,"tsunami":0}
         * geometry : {"type":"Point","coordinates":[-151.5129,63.1016,0]}
         */
        private final String type = "Feature";
        private FeatureProperties properties;
        private FeatureGeometry geometry;

        public FeatureProperties getProperties() {
            return properties;
        }

        public void setProperties(FeatureProperties properties) {
            this.properties = properties;
        }

        public FeatureGeometry getGeometry() {
            return geometry;
        }

        public void setGeometry(FeatureGeometry geometry) {
            this.geometry = geometry;
        }

        /**
         * 特征对象特征值
         */
        public static class FeatureProperties {
            private String id;
            private String imageName;

            public String getId() {
                return id;
            }

            public void setId(String id) {
                this.id = id;
            }

            public String getImageName() {
                return imageName;
            }

            public void setImageName(String imageName) {
                this.imageName = imageName;
            }
        }

        /**
         * 几何对象
         * 包含 coordinates 数组表示几何位置
         */
        public static class FeatureGeometry {
            /**
             * type : Point
             * coordinates : [-151.5129,63.1016,0]
             */
            private String type = "Point";
            private List<Double> coordinates;

            public String getType() {
                return type;
            }

            public void setType(String type) {
                this.type = type;
            }

            public List<Double> getCoordinates() {
                return coordinates;
            }

            public void setCoordinates(List<Double> coordinates) {
                this.coordinates = coordinates;
            }
        }
    }

    /**
     * 生成Json字符串
     */
    public String crateJson() {
        Gson gson = new Gson();
        return gson.toJson(this);
    }
}
